// Copyright (c) 2024, lanlian and contributors
// For license information, please see license.txt

frappe.ui.form.on("EMS Inspection Plan", {
	refresh(frm) {
        // 点检方案下推点检单
        frm.add_custom_button(__('Make EMS Inspection Doc'), () => {
            frappe.model.open_mapped_doc({
                method: "ems.equipment_managerment_system.doctype.ems_inspection_plan.ems_inspection_plan.make_inspection_doc",
                frm: cur_frm,
                freeze_message: __("Creating Task ...")
            });
        });
        // 从指定的点检模版复制点检项目,逐行添加至子表
        frm.fields_dict["items"].grid.add_custom_button(__('Get From Template'), 
			function() {
                frappe.db.get_doc('EMS Inspection Plan Template', frm.doc.template_title)
                .then(doc => {
                    doc.items.forEach(item => {
                        frm.add_child('items', {
                            inspection_item: item.inspection_item,
                            description: item.description,
                            inspection_method: item.inspection_method,
                            inspection_tool: item.inspection_tool
                        });
                    })
                })
                .then( () => { frm.refresh_field('items');})
        });
        frm.fields_dict["items"].grid.grid_buttons.find('.btn-custom').removeClass('btn-default').addClass('btn-primary');
	},
});
